package dem1;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-07-25
 * Time: 9:40
 */
public class Search extends Work{
    @Override
    public void work(DataSource dataSource) throws SQLException {
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test2?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        Connection connection = (Connection) dataSource.getConnection();

        String str = "select * from student";
        PreparedStatement statement = connection.prepareStatement(str);

        ResultSet resultSet = statement.executeQuery();

        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");

            System.out.println("id" + id + ", name" + name);

        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}
